#!/usr/bin/env perl
#
#       twapake.pl
#
#       Copyright 2010-2011 Frédéric Galusik <fredg~at~salixos~dot~org>
#       License: BSD Revised
#
###############################################################################

use strict;
use warnings;
use Getopt::Long;
use Curses::UI;

use Twapake::Twapake;
use Twapake::Twacurse;

( $< == 0 ) or die "Sorry, but only root can run this script.\n";

my $VERSION = 0.5;

my $Twapake = Twapake->new();

#
# help - usage
#
sub usage {
    print "Usage: $0 [options]\n\n";
    print "Options:\n";
    print " -l | --logfile\t\tprint snapshot logfile to /var/log/twapake/\n";
    print " -i | --installed\tprint a sorted list of packages\n";
    print " -n | --nlpkg\t\tused with -i or -r, choose the last N installed packages\n";
    print " -r | --remove\t\tremove the last N installed packages\n";
    print " -s | --since\t\tprint today installed packages\n";
    print " -c | --choosedate\tused with -s, choose the date\n";
    print " -u | --ui\t\tlaunch the Curses Interface for an easy removal\n";
    print " -v | --version\t\tprint twapake version\n";
    print " -h | --help\t\tprint this message\n";
    exit 0;
    }

#
# CLI options
#
my $version;
my $help;
my $logfile;
my $installed;
my $since;
my $remove;
my $ui;
GetOptions(
    'version'      =>  \$version,
    'help'         =>  \&usage,
    'logfile'      =>  \$logfile,
    'installed'    =>  \$installed,
    'nlpkg=i'      =>  \$Twapake::nlpkg,
    'remove'       =>  \$remove,
    'since'        =>  \$since,
    'choosedate=s' =>  \$Twapake::choosedate,
    'ui'           =>  \$ui,
    );
if ($version) {
    print "Twapake, version $VERSION\n";
    print "Copyright 2010-2012 Frédéric Galusik\n";
    print "http://www.salixos.org/wiki/index.php/Twapake\n";
    exit 0;
    }
if ($logfile) {
    Twapake->twalog();
    print "Writing $Twapake::twa_path/${Twapake::nowfile}_twapake.log ...\n";
    exit 0;
    }
if ($installed) {
    Twapake->sortmtimelist();
    exit 0;
    }
if ($remove) {
    Twapake->rmpkg();
    exit 0;
    }
if ($since) {
    Twapake->checkdate();
    Twapake->installedsince();
    exit 0;
    }
if ($ui) {
    Twapake->checkrm();
    Twacurse->uic();
    }
else {
    die "Wrong usage, run twapake --help for information.\n";
    }
